package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.dao.cargo.FactoryProductDao;
import cn.itcast.domain.cargo.FactoryProduct;
import cn.itcast.domain.cargo.FactoryProductExample;
import cn.itcast.service.cargo.FactoryProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class FactoryProductServiceImpl implements FactoryProductService {

    @Autowired
    private FactoryProductDao factoryProductDao;

    @Autowired
    private FactoryDao factoryDao;

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {

        //查询时排序
        FactoryProductExample example = new FactoryProductExample();
        example.setOrderByClause("create_time desc");

        PageHelper.startPage(pageNum,pageSize);
        List<FactoryProduct> list = factoryProductDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public void save(FactoryProduct factoryProduct) {

        //设置体积
        factoryProduct.setBulk(factoryProduct.getSizeHeigth() * factoryProduct.getSizeWidth() * factoryProduct.getSizeLength());

        //保存
        factoryProductDao.insertSelective(factoryProduct);
    }

    @Override
    public FactoryProduct findById(String id) {
        return factoryProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void update(FactoryProduct factoryProduct) {

        factoryProductDao.updateByPrimaryKeySelective(factoryProduct);
    }

    @Override
    public List<FactoryProduct> findAll(FactoryProductExample factoryProductExample) {
        List<FactoryProduct> list = factoryProductDao.selectByExample(factoryProductExample);
        return list;
    }

    @Override
    public void delete(String id) {
        factoryProductDao.deleteByPrimaryKey(id);
    }

}
